﻿// <copyright file="PagedList.xaml.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace Channel9Kinect
{
    #region

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    #endregion

    /// <summary>
    ///   Interaction logic for PagedList.xaml
    /// </summary>
    public partial class PagedList : UserControl
    {
        #region Constants and Fields

        /// <summary>
        ///   DefaultImage depedency property.
        /// </summary>
        public static readonly DependencyProperty DefaultImageProperty = DependencyProperty.Register("DefaultImage", typeof(Uri), typeof(PagedList), new PropertyMetadata((d, e) => ((PagedList)d).OnDefaultImageChanged((Uri)e.OldValue, (Uri)e.NewValue)));

        /// <summary>
        ///   FeedUri depedency property.
        /// </summary>
        public static readonly DependencyProperty FeedUriProperty = DependencyProperty.Register("FeedUri", typeof(Uri), typeof(PagedList), new PropertyMetadata((d, e) => ((PagedList)d).OnFeedUriChanged((Uri)e.OldValue, (Uri)e.NewValue)));

        /// <summary>
        ///   ItemCommand depedency property.
        /// </summary>
        public static readonly DependencyProperty ItemCommandProperty = DependencyProperty.Register("ItemCommand", typeof(ICommand), typeof(PagedList), new PropertyMetadata((d, e) => ((PagedList)d).OnItemCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue)));

        private readonly PagedListViewModel vm;

        #endregion

        #region Constructors and Destructors

        public PagedList()
        {
            this.InitializeComponent();

            this.vm = (PagedListViewModel)this.Resources["pagedListVm"];
        }

        #endregion

        #region Public Properties

        public Uri DefaultImage
        {
            get
            {
                return (Uri)this.GetValue(DefaultImageProperty);
            }

            set
            {
                this.SetValue(DefaultImageProperty, value);
            }
        }

        public Uri FeedUri
        {
            get
            {
                return (Uri)this.GetValue(FeedUriProperty);
            }

            set
            {
                this.SetValue(FeedUriProperty, value);
            }
        }

        public ICommand ItemCommand
        {
            get
            {
                return (ICommand)this.GetValue(ItemCommandProperty);
            }

            set
            {
                this.SetValue(ItemCommandProperty, value);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        ///   handles the DefaultImageProperty changes.
        /// </summary>
        /// <param name = "oldValue">The old value.</param>
        /// <param name = "newValue">The new value.</param>
        private void OnDefaultImageChanged(Uri oldValue, Uri newValue)
        {
            this.vm.DefaultImage = newValue;
        }

        /// <summary>
        ///   handles the FeedUriProperty changes.
        /// </summary>
        /// <param name = "oldValue">The old value.</param>
        /// <param name = "newValue">The new value.</param>
        private void OnFeedUriChanged(Uri oldValue, Uri newValue)
        {
            this.vm.FeedUri = newValue;
        }

        /// <summary>
        ///   handles the ItemCommandProperty changes.
        /// </summary>
        /// <param name = "oldValue">The old value.</param>
        /// <param name = "newValue">The new value.</param>
        private void OnItemCommandChanged(ICommand oldValue, ICommand newValue)
        {
            this.vm.ItemCommand = newValue;
        }

        #endregion
    }
}